package com.hopster;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TimePicker;

public class EditAlarm extends Activity 
{
	protected static final Context Alarm = null;
	private Button save, discard, delete;
	private Spinner repeat_type, snooze_type, puzzle_type, alarm_type;
	private EditText name;
	private TimePicker time;
	private SeekBar	volume;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_alarm);
        
        time		= (TimePicker) findViewById(R.id.timePicker1);
        name		= (EditText) findViewById(R.id.editText1);
        repeat_type = (Spinner) findViewById(R.id.spinner1);
        snooze_type = (Spinner) findViewById(R.id.spinner2);
        puzzle_type = (Spinner) findViewById(R.id.spinner3);
        alarm_type  = (Spinner) findViewById(R.id.spinner4);
        volume		= (SeekBar) findViewById(R.id.seekBar1);
        save 		= (Button) findViewById(R.id.save);
        discard 	= (Button) findViewById(R.id.discard);
        delete		= (Button) findViewById(R.id.delete);
        
        Bundle alarms = getIntent().getExtras();
        final String alarmname = alarms.getString("name");
        int hour = alarms.getInt("hour");
        int minute = alarms.getInt("minute");
        EditText namefield = (EditText) findViewById(R.id.editText1);
        TimePicker timefield = (TimePicker) findViewById(R.id.timePicker1);
        namefield.setText(alarmname);
        timefield.setCurrentHour(hour);
        timefield.setCurrentMinute(minute);
        
        //Discard 
        discard.setOnClickListener(new View.OnClickListener()
        {
        	public void onClick(View v)
        	{
        		finish();
      		}
      	});    
        
        //Delete
        delete.setOnClickListener(new View.OnClickListener()
        {
        	public void onClick(View v)
        	{
        		AlarmService as = AlarmService.getInstance();
        		as.removeAlarm(alarmname);
        		finish();
      		}
      	});   
        
        //Save
        save.setOnClickListener(new View.OnClickListener()
        {
			public void onClick(View v)
			{
				String alarmName = name.getText().toString();
				int hour = time.getCurrentHour();	// 0-23
				int minute = time.getCurrentMinute();	// 0-59
				
				String puzzleType = puzzle_type.getSelectedItem().toString();
				PendingIntent intent = null;
				
				// TODO: Initialize 'intent' differently based on the selected puzzle type.
				// Right now only "Math" gives it a puzzle.
				if(puzzleType.equals("Math"))
				{
					Intent i = new Intent(EditAlarm.this, TesterActivity.class);
					int newID = AlarmService.getNewID();
					intent = PendingIntent.getActivity(EditAlarm.this, newID, i, 0);
				}
				
				if(puzzleType.equals("Logic"))
				{
					Intent i = new Intent(EditAlarm.this, LogicalMath.class);
					int newID = AlarmService.getNewID();
					intent = PendingIntent.getActivity(EditAlarm.this, newID, i, 0);
				}
				
				if(puzzleType.equals("None"))
				{
					Intent i = new Intent(EditAlarm.this, NoPuzzle.class);
					int newID = AlarmService.getNewID();
					intent = PendingIntent.getActivity(EditAlarm.this, newID, i, 0);
				}
				
				Alarm alarmToAdd = new Alarm(alarmName, hour, minute, intent);
				AlarmService as = AlarmService.getInstance();
				as.removeAlarm(alarmname);
				boolean success = as.addAlarm(alarmToAdd);
				
				
				if(!success)
				{
					new AlertDialog.Builder(EditAlarm.this).setTitle("Error!").setMessage("An alarm with this name already exists. Please change the name of the alarm.").setNeutralButton("Close", null).show();
				}
				else
				{
					finish();
				}
			}
        });
	}
	
}